package com.gec.controller;

import com.alibaba.fastjson.JSONObject;

import com.gec.domain.Leave;
import com.gec.domain.PageBean;
import com.gec.domain.User;
import com.gec.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author Aniki
 * @date 2021-12-3
 */
@Controller
@RequestMapping("/Leave")
public class LeaveController extends  BaseController{

    @Autowired
    private LeaveService leaveService;

    Map< String , User> userMap = null;
    public LeaveController(){
        System.out.println("+----------------------[初始化用户]--------------------------+");
         userMap = UserData.initData();
        System.out.println("+----------------------[初始化用户]--------------------------+");
    }

   /**用户申请请假*/
   @RequestMapping("/showLeaveForm")
   public  String showLeaveForm(){

        return "leave/leaveForm";
   }
   @RequestMapping(value  = "/submitLeave",produces = "text/html;charset=UTF-8")
   @ResponseBody
    public String submitLeave(Leave leave){
       String respTxt = "";
       JSONObject jsonObject = new JSONObject();
       try {
           leaveService.startProcess(leave);
           jsonObject.put("result","success");
           jsonObject.put("leave",leave);
           respTxt = jsonObject.toString();
       } catch (Exception e) {
           /**调用父类的的方法包装错误信息*/
           respTxt = setErrorMsg(e);
       }
       return respTxt;
   }

   @RequestMapping(value = "/jsonMyInitiate",produces = "text/html;charset=UTF-8")
   @ResponseBody
    public String jsonMyInitiate(@RequestParam("initiator")String initiator) {
       /*后面要做分页 上面要增加参数*/
       PageBean pBean = null;
       String respTxt = "";
       try {
           pBean = leaveService.queryMyInitiate(initiator);
           respTxt = packJSON(pBean);
       } catch (Exception e) {
           e.printStackTrace();
           respTxt = setErrorMsg(e);
       }
       return respTxt;
   }


    @RequestMapping("/myInitiate")
    public String myInitiate() {

        return "leave/myInitiate";
    }

   /**=======================查看待我审批的任务=========================*/
  @RequestMapping("/waitMyApprove")
   public String waitMyApprove(){
      return "leave/waitMyApprove";
   }

   @RequestMapping(value = "jsonWaitMyApprove",produces = "text/html;charset=UTF-8")
   @ResponseBody
    public String jsonWaitApprove(
      @RequestParam("assigneeId")String assigneeId,
      @RequestParam(value = "page",defaultValue = "1")Integer page,
      @RequestParam(value = "limit",defaultValue = "10 ")Integer limit
      ){
         PageBean pageBean =null;
       try {
           pageBean = leaveService.queryWaitMyApprove(assigneeId, page, limit);
           return packJSON(pageBean);
       } catch (Exception e) {
           e.printStackTrace();
           return setErrorMsg(e);
       }
   }

   @RequestMapping("/taskDetail")
    public String taskDetail(@RequestParam("taskId")String taskId, Model model){
      Leave leave = null;
       try {
           leave = leaveService.queryMyTaskByTaskId(taskId);
           //设置到模型域
           model.addAttribute("leave", leave);
           return "leave/taskDetail";
       } catch (Exception e) {
           e.printStackTrace();
           return "redirect:/error.jsp?msg=xx";
       }
   }
   /**--------------------提交我审批的任务---------------------------------*/
    /***
     * 映射：/Leave/doApprove
     *客户端提交以下参数
     * 1.deptID:部门ID
     * 2.appFlag:true(同意)/false(不同意)
     * 3.refuseReason:拒绝原因
     * 4.taskId:任务ID
     */
    @RequestMapping(value = "/doApprove",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public  String doApprove(@RequestParam Map map){
        JSONObject jsonObject = new JSONObject();
        String respTxt = "";
        try {
            leaveService.completeMyApprove(map);
            jsonObject.put("result","success");
            jsonObject.put("operation","doApprove");
            respTxt = jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
            respTxt = setErrorMsg(e);
        }
        return respTxt;
    }

}